Installing packages

library("sf")
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library("rinat")
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("tmap")
library("ggspatial")
library("mapview")

Biome data

veg <- st_read("data/Biomes_of_South_Africa_DEA_CSIR_2015-11-06_poly.shp")
## Reading layer `Biomes_of_South_Africa_DEA_CSIR_2015-11-06_poly' from data source `C:\Git repository\GIS\data\Biomes_of_South_Africa_DEA_CSIR_2015-11-06_poly.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1705 features and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -829014.6 ymin: -3850630 xmax: 776484.9 ymax: -2436433
## Projected CRS: Albers
veg$DN[veg$DN == "1"] <- "Dessert"
veg$DN[veg$DN == "2"] <- "Succulant Karoo"
veg$DN[veg$DN == "3"] <- "Nama Karoo"
veg$DN[veg$DN == "4"] <- "Fynboss"
veg$DN[veg$DN == "5"] <- "Albany Thicket"
veg$DN[veg$DN == "6"] <- "Grassland"
veg$DN[veg$DN == "7"] <- "Savanna"
veg$DN[veg$DN == "8"] <- "Forest"
veg$DN[veg$DN == "16"] <- "Indian Ocean Coastal Belt"

class(veg)
## [1] "sf"         "data.frame"
ggplot() + geom_sf(data = veg, aes(fill = DN),  color = "black", size = 0.01) + scale_fill_brewer(palette = "Set1")

V. karroo distribution

vk <- get_inat_obs(taxon_name = "Vachellia karroo", maxresults = 5000)

vk <- vk %>% filter(positional_accuracy<46 & 
                     latitude<0 &
                     !is.na(latitude) &
                     captive_cultivated == "false" &
                     quality_grade == "research")

vk <- st_as_sf(vk, coords = c("longitude", "latitude"), crs = 4326)

# Extract coordinates from the geometry column
vk_coords <- st_coordinates(vk)

vk <- st_transform(vk, crs = st_crs(veg))

filter.sa <- function(vk) { if(!inherits(vk, "sf")) { stop ("Error: vk must be  an sf object with point geometries")}                                   
  
filtered.vk <- st_intersection(vk, veg)  
return(filtered.vk)}

sa_vk <- filter.sa(vk)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
ggplot() + annotation_map_tile(type = "osm", progress = "none") + geom_sf(data=sa_vk)

Static map

ggplot() +
  geom_sf(data = veg, aes(fill = DN), color = "black", alpha = 0.5) +  
  geom_sf(data = sa_vk, color = "black", size = 0.8) +   
  labs(fill = "Biome") +
  theme_minimal()

Interactive map

mapview(veg, zcol = "DN", layer.name = "Biome") + mapview(sa_vk, col.regions = "black", layer.name = "V. karroo", cex = 2)